#!/bin/bash

#1 获取输入参数个数，如果没有参数，直接退出
FILE_COUNT=$#
if((FILE_COUNT!=1)); then
    echo "错误：传递文件参数错误！"
    echo "用法：storage_docker.sh trackerIp:port"
    exit;
fi
# 指定连接的 tracker 主机ip:port
TRACKER_IP_PORT=$1
# 创建数据目录
MODULE_PATH=/opt/module/fastdfs/storage
DOCKER_NAME=fastdfs_storage

# 删除数据目录及窗器
rm -rf $MODULE_PATH
if(( `docker ps -a | grep $DOCKER_NAME | wc -l` >0 ));then
    docker rm -f $DOCKER_NAME
fi

# 启动容器
docker run -d \
    --privileged=true \
    -v /etc/localtime:/etc/localtime:ro \
    -e TZ=Asia/Shanghai \
    --network=host \
    --name $DOCKER_NAME \
    -v $MODULE_PATH:/var/fdfs \
    -e TRACKER_SERVER=$TRACKER_IP_PORT \
    -e GROUP_NAME=group1 \
    delron/fastdfs storage

# 查看状态
docker logs $DOCKER_NAME

# 进入容器上传测试：
# [root@mini1 html]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf index.html
# group1/M00/00/00/wKg4ZV6ROzKAXMPqAAACZxUG5LY92.html
# 宿主机挂载目录（文件查看）
# [root@mini1 00]# pwd
# /opt/module/fastdfs/storage/data/00/00
# [root@mini1 00]# ll
# 总用量 4
# -rw-r--r-- 1 root root 615 4月  11 11:36 wKg4ZV6ROzKAXMPqAAACZxUG5LY92.html
# ================== 访问地址 ==========================
# nginx：默认端口是8888：http://192.168.56.101:8888/group1/M00/00/00/wKg4ZV6ROzKAXMPqAAACZxUG5LY92.html
# tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      10201/fdfs_storaged
# tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      10192/nginx: master
# tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      9941/fdfs_trackerd
# =============================================
# 默认nginx配置：/usr/local/nginx/conf/nginx.conf
# =============================================
# [root@mini1 conf]# cat nginx.conf | grep -v '#' | grep -v '^$'
# worker_processes  1;
# events {
#     worker_connections  1024;
# }
# http {
#     include       mime.types;
#     default_type  application/octet-stream;
#     sendfile        on;
#     keepalive_timeout  65;
#     server {
#         listen       8888;
#         server_name  localhost;
#         location ~/group[0-9]/ {
#             ngx_fastdfs_module;
#         }
#         error_page   500 502 503 504  /50x.html;
#         location = /50x.html {
#             root html;
#         }
#     }
# }


# 查看启动参数：如下
# /opt/module/fastdfs/storage/logs
# =============================================
# [2020-04-11 11:19:36] INFO - FastDFS v5.11
# base_path=/var/fdfs
# store_path_count=1
# subdir_count_per_path=256
# group_name=group1
# run_by_group=
# run_by_user=
# connect_timeout=30s
# network_timeout=60s
# port=23000
# bind_addr=
# client_bind=1
# max_connections=256
# accept_threads=1
# work_threads=4
# disk_rw_separated=1
# disk_reader_threads=1
# disk_writer_threads=1
# buff_size=256KB
# heart_beat_inter
# val=30s
# stat_report_interval=60s
# tracker_server_count=1
# sync_wait_msec=50ms
# sync_interval=0ms
# sync_start_time=00:00
# sync_end_t
# ime=23:59
# write_mark_file_freq=500
# allow_ip_count=-1
# file_distribute_path_mode=0
# file_distribute_rotate_count=100
# fsync_after_w
# ritten_bytes=0
# sync_log_buff_interval=10s
# sync_binlog_buff_interval=10s
# sync_stat_file_interval=300s
# thread_stack_size=512 KB
# upload_priority=10
# if_alias_prefix=
# check_file_duplicate=0
# file_signature_method=hash
# FDHT group count=0
# FDHT server count=0
# FDHT key_namespace=
# FDHT keep_alive=0
# HTTP server port=8888
# domain name=
# use_access_log=0
# rotate_access_log=0
# access_log_rotate_
# time=00:00
# rotate_error_log=0
# error_log_rotate_time=00:00
# rotate_access_log_size=0
# rotate_error_log_size=0
# log_file_keep_days=0
# file_sync_skip_invalid_record=0
# use_connection_pool=0
# g_connection_pool_max_idle_time=3600s
# data path: /var/fdfs/data
# mkdir sub dir...
# mkdir data path: 00 ...
# mkdir data path: 01 ...
# mkdir data path: 02 ...
# mkdir data path: 03 ...

